<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;

class BaseController extends Controller {

    //put your code here

 public function _initialize() {

        $ob = M("config");
        $config = $ob->field('name,data')->select();
        foreach ($config as $key => $value) {
            C($value['name'], $value['data']);
        }
         //增加统计量
        addData();
        $user_id = session('user_id');
        if (!empty($user_id)) {
            //获取用户购物车里的数据
            $where['user_id'] = session('user_id');
            $cartNum = M('cart')->where($where)->count();
        } else {
            $cartNum = 0;
        }
        
        //搜索关键词
        $keywords = M('keywords')->order('k_order desc')->select();
        $this->assign("keywords",$keywords);
        
        
        //商品分类
        if(S('tdd_category')){
            $category = S('tdd_category');
        }else{
            //拿出一级分类与$class1对比  在数组中的则分类下有商品，把所有有商品的分类放入$category中
            $category = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id = 0 and is_show=1')->limit(10)->order('sort_order asc')->select();

            foreach ($category as $key => $value) {
                //拿出一级分类查询二级，并用同上的方法判断分类中是否有商品
                $cat2 = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id ='.$value['cat_id'])->select();
                $category[$key]['class2'] = $cat2;
                foreach ($category[$key]['class2'] as $k => $v) {
                    //拿出二级分类查询三级，并用同上的方法判断分类中是否有商品
                    $cat3  = M('category')->field('cat_id,cat_name,parent_id,keywords,cat_desc')->where('parent_id ='.$v['cat_id'])->select();
                    $category[$key]['class2'][$k]['class3'] = $cat3;
                }
            }
            S('tdd_category',$category);
        }
        $this->assign("category",$category);

        //中部导航
        $middle_navigation = M("navigation")->where(array("nav_location"=>1))->order('nav_sort')->select();
//        p($middle_navigation);die;
        $this->assign('middle_navigation',$middle_navigation);

        //获取菜单导航
        $meau = M('navigation')->where('nav_location=1')->select();
        $this->assign('meau', $meau);
        $this->assign('cartNum', $cartNum);
        $this->assign('cartList', $cartList);


        if(session("user_id")){
            //菜单---购物券
            $user_id = session("user_id");
            $where['voucher_owner_id'] = $user_id;
            $where['voucher_state'] = 1;
            $where['voucher_end_date'] = array('gt',time());
            $p_voucher = M('p_voucher')->where($where)->select();
            $this->assign('p_voucher',$p_voucher);

            //菜单---浏览记录
            $goods_browse = M('goods_browse')->where(array('member_id'=>session("user_id")))->order('browsetime desc')->field('goods_id')->select();
            if($goods_browse){
                foreach($goods_browse as $v){
                    $arr[] = $v['goods_id'];
                }
                $where['goods_sale'] = 1;
                $where['goods_state'] = 1;
                $where['goods_id'] = array('in',$arr);
                $goods = M('goods')->where($where)->field('goods_id,goods_thumb,goods_title,goods_price')->select();
                foreach($goods_browse as $k=>$v){
                    foreach($goods as $o){
                        if($v['goods_id'] == $o['goods_id']){
                            $goods_browse[$k]['goods_thumb'] = $o['goods_thumb'];
                            $goods_browse[$k]['goods_title'] = $o['goods_title'];
                            $goods_browse[$k]['goods_price'] = $o['goods_price'];
                        }
                    }
                }
                $this->assign('goods_browse',$goods_browse);
            }

        }

        //公共bootom(写死)
        $article_cat = M('Article_cat')->where(array('cat_id'=>array('in',array(95,94,89,92,96,98))))->select();
        $tree = new \Libs\Tree\Tree();
        $arr = $tree->channelLevel($article_cat, '0', '&nbsp;', 'cat_id', 'parent_id');
        $this->assign("arr_a", $arr);
        $article = M('Article')->where(array('cat_id'=>array('in',array(95,94,92,96,98))))->select();
        $this->assign("article_a", $article);
//        die;
//     p($arr);die;
    }


}
